package com.xy.biz.fd.service.impl;

import com.xy.biz.fd.domain.bo.DocumentBO;
import com.xy.biz.fd.domain.bo.FolderBO;
import com.xy.biz.fd.domain.bo.RepositoryBO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author 于振晗
 * @title: EventBus
 * @description: TODO
 * @date 2021年01月30日
 * @path com.xy.biz.fd.service.impl
 */
@Service
public class DocumentEventBus {

    private Supplier<RepositoryBO> rubbishRepositoryInitHandler;
    private List<Consumer<Long>> afterFolderDeletionHandlerList = new CopyOnWriteArrayList<>();
    private List<Consumer<Long>> afterDocumentEditHandlerList = new CopyOnWriteArrayList<>();
    private List<Consumer<DocumentBO>> afterDocumentMoveHandlerList = new CopyOnWriteArrayList<>();
    private List<Consumer<FolderBO>> afterFolderMoveHandlerList = new CopyOnWriteArrayList<>();
    private List<Consumer<Long>> afterFolderDelHandlerList = new CopyOnWriteArrayList<>();
    private List<Consumer<FolderBO>> afterFolderRecoverHandlerList = new CopyOnWriteArrayList<>();
    /**
     * 查看文档删除状态
     */
    private List<Consumer<Long>> afterDocumentStatusHandlerList = new CopyOnWriteArrayList<>();

    /**
     * 查看文件夹删除状态
     */
    private List<Consumer<Long>> afterFolderStatusHandlerList = new CopyOnWriteArrayList<>();

    public void setRubbishRepositoryInitHandler(Supplier<RepositoryBO> handler) {
        if (handler != null) {
            rubbishRepositoryInitHandler = handler;
        }
    }

    public RepositoryBO getRubbishRepository() {
        if (rubbishRepositoryInitHandler != null) {
            return rubbishRepositoryInitHandler.get();
        }
        throw new IllegalStateException("The rubbishRepositoryInitHandler is not init yet.");
    }

    public void addFolderDeletionHandler(Consumer<Long> handler) {
        afterFolderDeletionHandlerList.add(handler);
    }

    public void addDocumentEditHandler(Consumer<Long> handler) {
        afterDocumentEditHandlerList.add(handler);
    }

    public void addDocumentMoveHandler(Consumer<DocumentBO> handler) {
        afterDocumentMoveHandlerList.add(handler);
    }

    public void addFolderMoveHandler(Consumer<FolderBO> handler) {
        afterFolderMoveHandlerList.add(handler);
    }

    @Transactional
    public void addFolderRecoverHandle(Consumer<FolderBO> handler) {
        afterFolderRecoverHandlerList.add(handler);
    }

    @Transactional
    public void addFolderDelHandler(Consumer<Long> handler) {
        afterFolderDelHandlerList.add(handler);
    }

    @Transactional
    public void fireFolderDelHandler(Long folderId) {
        afterFolderDelHandlerList.forEach(e -> e.accept(folderId));
    }

    @Transactional
    public void fireFolderRecoverHandler(FolderBO folderBO) {
        afterFolderRecoverHandlerList.forEach(e -> e.accept(folderBO));
    }

    @Transactional
    public void fireDocumentEditHandler(Long documentId) {
        afterDocumentEditHandlerList.forEach(e -> e.accept(documentId));
    }

    @Transactional
    public void fireDocumentMoveHandler(DocumentBO documentBO) {
        afterDocumentMoveHandlerList.forEach(e -> e.accept(documentBO));
    }

    public void fireFolderDeletionHandler(Long folderId) {
        afterFolderDeletionHandlerList.forEach(e -> e.accept(folderId));
    }

    @Transactional
    public void fireFoldertMoveHandler(FolderBO folderBO) {
        afterFolderMoveHandlerList.forEach(e -> e.accept(folderBO));
    }

    @Transactional
    public void addDocumentStatusHandler(Consumer<Long> handler) {
        afterDocumentStatusHandlerList.add(handler);
    }

    @Transactional
    public void fireDocumentStatusHandler(Long documentId) {
        afterDocumentStatusHandlerList.forEach(e -> e.accept(documentId));
    }

    @Transactional
    public void addFolderStatusHandler(Consumer<Long> handler) {
        afterFolderStatusHandlerList.add(handler);
    }

    @Transactional
    public void fireFolderStatusHandler(Long folderId) {
        afterFolderStatusHandlerList.forEach(e -> e.accept(folderId));
    }
}
